package com.intel.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.intel.model.ResponseHead;

import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: ivan.yao<haif.yao@gmail.com>
 * @date: 16/4/13.
 */
public class JsonRevetUtils {


    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }


    public static <T>String beanToJson(T t) {
        String json = "";
        StringWriter writer = null;
        try {
            writer = new StringWriter();

            objectMapper.writeValue(writer, t);
            json = writer.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return json;
    }


    public static <T>T parseJson(String jsonStr,Class<T> valueType){
        T t =  null;
        try {
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            t = objectMapper.readValue(jsonStr,valueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    public static <T> T parseJson(String jsonStr,TypeReference valueType){
        T t =  null;
        try {
            t = objectMapper.readValue(jsonStr, valueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    public static void main(String[] args) {

        String x = "[\n" +
                "    {\n" +
                "        \"result\": {\n" +
                "            \"total\": 1, \n" +
                "            \"dataList\": [\n" +
                "                {\n" +
                "                    \"hello\": \"YP1000415\", \n" +
                "                    \"PRODUCT_NAME\": \"强力枇杷露\", \n" +
                "                    \"ENGLISH_NAME\": \"WSSERJN\", \n" +
                "                    \"APPROVAL_NUMBER\": \"国药准字Z20033116\", \n" +
                "                    \"TRADE_NAME\": \"维生素E软胶囊\", \n" +
                "                    \"BASE_UNIT\": \"盒\", \n" +
                "                    \"TYPY\": \"软胶囊（胶丸）\", \n" +
                "                    \"SPEC\": \"100mg*30粒\", \n" +
                "                    \"CORP_NAME\": \"河北瑞生药业有限公司\", \n" +
                "                    \"IMPORT_REGISTER_NUMBER\": \"\", \n" +
                "                    \"IS_OLD\": \"2017112706\", \n" +
                "                    \"TYPE_VALUE\": \"否\"\n" +
                "                }\n" +
                "            ], \n" +
                "            \"totalpage\": \"\"\n" +
                "        }, \n" +
                "        \"msgDesc\": \"成功\", \n" +
                "        \"msgId\": \"000\", \n" +
                "        \"success\": false\n" +
                "    }\n" +
                "]";
        parseJson(x, new TypeReference<List<ResponseHead>>(){});
    }
}
